package com.blog.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blog.constant.MsgConstant;
import com.blog.pojo.dto.CategoryDTO;
import com.blog.pojo.entity.Category;
import com.blog.pojo.entity.PageBean;
import com.blog.result.Result;
import com.blog.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  分类管理
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param categoryName
     * @param categoryAlias
     * @return
     */
    @GetMapping("/page")
    public Result<PageBean> page(
        @RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "10") Integer pageSize,
        @RequestParam(required = false) String categoryName,
        @RequestParam(required = false) String categoryAlias
    ) {
        log.info("开始分页查询分类：{}, {}, {}, {}", pageNum, pageSize, categoryName, categoryAlias);
        try {
            IPage<Category> pageResult = categoryService.pageQuery(pageNum, pageSize, categoryName, categoryAlias);
            return Result.success(new PageBean<>(pageResult.getTotal(), pageResult.getRecords()));
        } catch (Exception e) {
            return Result.error("异常");
        }
    }

    /**
     * 获取全部分类信息
     * @return
     */
    @GetMapping("/all")
    public Result<List<Category>> getCategoryAll() {
        log.info("开始获取全部的分类信息");
        try {
            List<Category> categoryList = categoryService.getCategoryAll();
            return Result.success(categoryList);
        } catch (Exception e) {
            return Result.error(MsgConstant.CATEGORY_SELECT_ERROR);
        }
    }

    /**
     * 根据id获取分类名称
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<String> getCategoryName(@PathVariable Long id){
        log.info("开始根据id获取分类名称");

        try {
            String categoryName = categoryService.getCategoryName(id);
            return Result.success(categoryName);
        } catch (Exception e) {
            return Result.error(MsgConstant.CATEGORY_SELECT_ERROR);
        }
    }

    /**
     * 添加文章分类
     * @param categoryDTO
     * @return
     */
    @PostMapping("/add")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("开始添加分类：{}", categoryDTO);
        
        try {
            categoryService.savaOne(categoryDTO);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.error("添加分类失败：", e);
            return Result.error("添加分类失败");
        }
    }

    /**
     * 单独删除某个分类
     * @return
     */
    @DeleteMapping("{id}")
    public Result deleteOne(@PathVariable Long id) {
        log.info("开始删除分类：{}", id);
        
        try {
            categoryService.deleteOne(id);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (RuntimeException e) {
            log.error("删除分类失败：", e);
            return Result.error(e.getMessage());  // 返回具体错误信息
        }
    }

    /**
     * 批量删除分类
     * @param ids
     * @return
     */
    @DeleteMapping("/batch")
    public Result deleteBacth(@RequestBody List<Long> ids){
        log.info("开始批量删除分类 {}", ids);

        try {
            categoryService.deleteBatch(ids);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            return Result.error("批量删除失败");
        }
    }

    /**
     * 更改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping()
    public Result update(@RequestBody CategoryDTO categoryDTO){
        log.info("开始修改分类{}", categoryDTO);

        try {
            categoryService.updateOne(categoryDTO);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            return Result.error(MsgConstant.CATEGORY_UPDATE_ERROR);
        }

    }
}
